package com.sky.service.impl;

import com.github.pagehelper.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.CategoryCreateException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult selectCategoryByName(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> categoryList = categoryMapper.selectCategoryByName(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        PageInfo<Category> categoryPageInfo = PageInfo.of(categoryList);
        return new PageResult(categoryPageInfo.getTotal(),categoryPageInfo.getList());
    }

    @Override
    public void create(CategoryDTO categoryDTO) {
        Category category = new Category();
        Integer countName = categoryMapper.selectCountNameById(categoryDTO.getName());
        Integer countSort = categoryMapper.selectCountSortById(categoryDTO.getSort());
        if(countName > 0){
            throw new CategoryCreateException(MessageConstant.REPEAT_CATEGORY_NAME);
        }
        if(countSort > 0){
            throw new CategoryCreateException(MessageConstant.REPEAT_CATEGORY_TYPE);
        }
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);

        categoryMapper.insertCategory(category);
    }

    @Override
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        Integer count1 = categoryMapper.selectCountNameByIdNotSelf(categoryDTO.getId(),categoryDTO.getName());
        Integer count2 = categoryMapper.selectCountSortByIdNotSelf(categoryDTO.getId(),categoryDTO.getSort());
        if(count1 > 0){
            throw new CategoryCreateException(MessageConstant.REPEAT_CATEGORY_NAME);
        }
        if(count2 > 0){
            throw new CategoryCreateException(MessageConstant.REPEAT_CATEGORY_TYPE);
        }
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.updateCategory(category);
    }

    @Override
    public void updateCategoryStatus(Long id, Integer status) {
        Category category = categoryMapper.selectCategoryById(id);
        category.setStatus(status);
        categoryMapper.updateCategory(category);
    }

    @Override
    public List<Category> selectCategoryByType(Integer type) {
        List<Category> categoryList = categoryMapper.selectCategoryByType(type);
        return categoryList;
    }

    @Override
    public List<Category> selectCategory() {
        List<Category> categoryList = categoryMapper.selectCategory();
        return categoryList;
    }
}
